//
//  CZMenuButton.m
//  i0S007通用项目
//
//  Created by Apple on 16/2/19.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "CZMenuButton.h"
#import "CZMenuItem.h"

@implementation CZMenuButton

+ (instancetype)menuButtonWithMenuItem:(CZMenuItem *)menuItem {
    return [[self alloc] initWithMenuItem:menuItem];
}

- (instancetype)initWithMenuItem:(CZMenuItem *)menuItem {
    if (self = [super init]) {
        // 记录下menuItem模型
        _menuItem = menuItem;
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI {
    // 设置按钮图片
    [self setImage:[UIImage imageNamed:self.menuItem.imageName] forState:UIControlStateNormal];
    [self setBackgroundImage:[UIImage imageNamed:@"tabbar_separate_selected_bg"] forState:UIControlStateSelected];
    
    if (!self.menuItem.isComposeButton) {
        // 不是撰写相关的按钮
        // 设置底部分割线
        UIImageView *separatorView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabbar_separate_line"]];
        [self addSubview:separatorView];
        [separatorView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.trailing.bottom.equalTo(self);
            make.height.equalTo(@(2));
        }];
        
        [self setupButton:YES];
    } else {
        // 设置右边分割线
        UIImageView *separatorView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabbar_separate_ugc_line_v"]];
        [self addSubview:separatorView];
        
        [separatorView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.trailing.equalTo(self);
            make.width.equalTo(@(2));
        }];
    }
}

/// 当屏幕方向改变的时候,根据屏幕方向设置按钮的相关属性
- (void)setupButton:(BOOL)isLanscape {
    if (isLanscape) {
        // 设置标题
        [self setTitle:self.menuItem.title forState:UIControlStateNormal];
        
        // 设置按钮的对齐模式
        self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        
        // 设置按钮内容的左边间距
        self.contentEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);
        
        // 设置title的左边间距
        self.titleEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);
    } else {
        // 设置标题
        [self setTitle:nil forState:UIControlStateNormal];
        
        // 设置按钮的对齐模式
        self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        
        // 设置按钮内容的左边间距
        self.contentEdgeInsets = UIEdgeInsetsZero;
        
        // 设置title的左边间距
        self.titleEdgeInsets = UIEdgeInsetsZero;
    }
}
@end
